package com.examples {
	import com.greensock.TweenMax;
	import com.greensock.easing.Back;
	import com.greensock.plugins.TintPlugin;
	import com.greensock.plugins.TweenPlugin;

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	TweenPlugin.activate([TintPlugin]);
	public class TestApp extends Sprite {
		private var _items : Array = new Array;
		private var _spacing : Number;
		private var _position : Number;
		private var _leftArrow : ArrowButton;
		private var _rightArrow : ArrowButton;
		private var _background : Background;
		private var _item : Item;

		public function TestApp() {
			makeBackground();
			createItems();
			createArrowButtons();
		}

		private function makeBackground() : void {
			_background = new Background;
			this.addChild(_background);
		}

		private function createArrowButtons() : void {
			_leftArrow = new ArrowButton();
			_leftArrow.y = 120;
			addChild(_leftArrow);
			_leftArrow.addEventListener(MouseEvent.CLICK, previousClickHandler);

			_rightArrow = new ArrowButton();
			_rightArrow.x = 80;
			_rightArrow.y = 120;
			addChild(_rightArrow);
			_rightArrow.addEventListener(MouseEvent.CLICK, nextClickHandler);
		}

		private function previousClickHandler(event : MouseEvent) : void {
			selection(_position - 1);
			_rightArrow.addEventListener(MouseEvent.CLICK, nextClickHandler);
			_rightArrow.arrowBtnAdd();
			_item.itemAdd();

			if (_position == 0 ) {
				_leftArrow.removeEventListener(MouseEvent.CLICK, previousClickHandler);
			}
		}

		private function nextClickHandler(event : MouseEvent) : void {
			selection(_position + 1);
			_leftArrow.addEventListener(MouseEvent.CLICK, previousClickHandler);

			if (_position == 4) {
				_rightArrow.removeEventListener(MouseEvent.CLICK, nextClickHandler);
			}
		}

		private function createItems() : void {
			_items = [];
			_spacing = 10;

			for (var i : int = 0 ; i < 5 ;i++) {
				_item = new Item();
				_item.x = i * (_item.width + _spacing);
				_item.addEventListener(MouseEvent.CLICK, itemClickHandler);
				addChild(_item);
				_items.push(_item);
			}
		}

		private function itemClickHandler(event : MouseEvent) : void {
			var item : Item = event.currentTarget as Item;
			var resultOfIndex : int = _items.indexOf(item);

			selection(resultOfIndex);

			if (_position == 4) {
				item.itemDispose();
				item.removeEventListener(MouseEvent.CLICK, itemClickHandler);
				_rightArrow.arrowBtnDispose();
				_rightArrow.removeEventListener(MouseEvent.CLICK, nextClickHandler);
			} else if (_position > 4 ) {
				// item.itemOutHandlerAdd();
				item.addEventListener(MouseEvent.CLICK, itemClickHandler);
				// _rightArrow.addEventListener(MouseEvent.CLICK, nextClickHandler);
			}
		}

		private function selection(position : int) : void {
			_position = position;

			for (var i : int = 0; i < _items.length ; i++) {
				var item : Item = _items[i];
				TweenMax.to(item, .5, {x:(i - _position) * (item.width + _spacing), ease:Back.easeIn});
				// TweenMax.to(_items[_position], .5, {alpha:0.5, tint:0xdce44f});
			}
		}
	}
}
